<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\User;
use Hash;
use DB;
class HomeUserController extends Controller
{
   /**
    * 找回密码的显示页面
    */
   public function password()
   {
        return view('homes.password');
   }

   /**
    * 找回密码的执行方法
    */

   public function doPassword(Request $request)
   {
   		
   		$this->validate($request, [
             	'userName' => 'required|regex:/^\w{6,20}$/',
                'password' => 'required|regex:/^\S{6,20}$/|same:repassword',
                
            ],[
                'userName.required'=>'对不起用户名必填',
                'userName.regex'=>'用户名格式不正确',
                
                'password.required' => '密码必填',
                'password.regex' => '密码格式不正确',
                'password.same' => '两次密码不一致'
               
            ]);
   		$user = new User;
   		//获取用户名
   		 $userName = $request->userName;
   		
   		 //  //读取数据库 用户名
   		 
         //获取用户的id
          $id = DB::table('users')->where('userName',$userName)->value('id');
       
        //判断
        if(empty($id)) {
            return back()->with('info','用户名不存在');
        }

        $password = $user -> password = Hash::make($request->input('password'));

	      $success =  DB::table('users')
	            ->where('id', $id)
	            ->update(['password' => $password]);
	       if($success){
	       		return redirect('/login')->with('info','找回密码成功请登录');
	       }else{
	       		return back()->with('info','找回密码失败请重试');
	       }
   }


}
